<?
require_once('functions.php');

function GetOrders()
{
		
		$con = GetConnection();
		$db = mysql_select_db("n02447784_db",$con);
        $sql = "SELECT * from Orders";
		return $results = mysql_query($sql,$con);
		
}
function BlankOrder()
{
    return array('id'=>null, 'created_at'=>null,'updated_at'=>null,'first_name'=>null,'last_name'=>null);
}
function GetOrder($id)
{
        $con = GetConnection();
		$db = mysql_select_db("n02447784_db",$con);
        $sql = "SELECT * FROM Orders WHERE id=$id";
        return $results = mysql_query($sql, $con);	
}  
function DeleteOrder($id)
{
        $con = GetConnection();
		$db = mysql_select_db("n02447784_db",$con);
        mysql_query("DELETE FROM Orders WHERE id=$id");
        return $results = mysql_query($sql, $con);	
}   
function SaveOrder(&$row)
{
    $con = GetConnection();
	$db = mysql_select_db("n02447784_db",$con);
    if(empty($row['id']))
    { 
        $sql =  "INSERT INTO Orders(created_at, first_name, last_name, b_address, b_city, b_state, b_zip, s_address, s_city, s_state, s_zip, price, card_number, ex_date) 
               VALUES (NOW(), '$row[first_name]', '$row[last_name]', '$row[b_address]', '$row[b_city]', '$row[b_state]','$row[b_zip]', '$row[s_address]', '$row[s_city]', '$row[s_state]', '$row[s_zip]', '$row[price]',
                '$row[card_number], '$row[ex_date]',  ) ";
    
    }
    	$return = mysql_query($sql,$con);
				
				mysql_close($con);
				return $return;
}
function ValidateOrder($row)
{
    $errors = array();
	if(empty($row['id'])) $errors['id'] = 'Order Name is required';
    if(empty($row['price'])) $errors['price'] = 'Price is required';
    if(empty($row['quantity'])) $errors['quantity'] = 'Quantity is required';  
    return count($errors) == 0 ? true : $errors;
}
?> 